#include <iostream>
#include <mlt++/Mlt.h>

int getFrame(double sec_time, double fps){
	return int(sec_time*fps + 0.5);
}

int main(int argc, char** argv){
	Mlt::Factory::init(NULL);
	Mlt::Profile *m_profile = new Mlt::Profile ("square_ntsc_wide");
	//Mlt::Profile *m_profile = new Mlt::Profile ("dv_pal");
	Mlt::Playlist *playlist = new Mlt::Playlist();
	Mlt::Producer *producer = new Mlt::Producer (*m_profile, "vamp.mp4");
	double fps = producer->get_fps();
	playlist->append(*producer);
	int cutPos1deb = getFrame(5, fps);
	int cutPos1fin = getFrame(8, fps);
	int cutPos2deb = getFrame(12, fps);
	int cutPos2fin = getFrame(15, fps);
	//Peut etre qu'il faudrait faire un lock : cf kdenlive renderer.cpp mlt_lock_service)

	int clipIndex2fin = playlist->get_clip_index_at(cutPos2fin);
	playlist->split_at(cutPos2fin);

	int clipIndex2deb = playlist->get_clip_index_at(cutPos2deb);
	playlist->split_at(cutPos2deb);

	int clipIndex1fin = playlist->get_clip_index_at(cutPos1fin);
	playlist->split_at(cutPos1fin);

	int clipIndex1deb = playlist->get_clip_index_at(cutPos1deb);
	playlist->split_at(cutPos1deb);

	playlist->remove(4);
	playlist->remove(2);
	playlist->remove(0);

	Mlt::Consumer *m_consumer = new Mlt::Consumer (*m_profile, "avformat", "test.dvd");
	//m_consumer->set ("play.audio_buffer", 4096);
	m_consumer->connect(*playlist);
	//m_consumer->start();
	m_consumer->run();
	std::cout << "Finished" << std::endl;
	return 0;
}

